package com.gopay.scheduler.service.cbrmb.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.gopay.common.cps.dao.cbrmb.CbRmbCustomsPaymentDAO;
import com.gopay.common.domain.cbrmb.customs.CbRmbCustomsPayment;
import com.gopay.common.domain.cbrmb.customs.base.CEB412Message;
import com.gopay.common.domain.cbrmb.customs.base.PaymentReturn;
import com.gopay.common.util.cbrmb.FtpUtils;
import com.gopay.common.util.cbrmb.customs.JaxbTools;
import com.gopay.scheduler.service.cbrmb.CbRmbCustomsPaymentService;

@Service("cbRmbCustomsPaymentService")
public class CbRmbCustomsPaymentServiceImpl implements CbRmbCustomsPaymentService {

	private Logger logger = Logger.getLogger(getClass());

	@Resource(name = "cbRmbCustomsPaymentDAO")
	private CbRmbCustomsPaymentDAO cbRmbCustomsPaymentDAO;

	@Resource(name = "customsConfig")
	private Map<String, String> customsConfig;

	/**
	 * 获取待报关信息
	 */
	@Override
	public List<CbRmbCustomsPayment> getUndeclaredOrder() {
		return cbRmbCustomsPaymentDAO.getUndeclaredOrder();
	}

	/**
	 * 获取超时失效信息
	 */
	@Override
	public List<CbRmbCustomsPayment> getTimeOutOrder() {
		return cbRmbCustomsPaymentDAO.getTimeOutOrder();
	}

	/**
	 * 更新报关信息
	 */
	@Override
	public void updateOrder(CbRmbCustomsPayment cbRmbCustomsPayment) {
		cbRmbCustomsPaymentDAO.update(cbRmbCustomsPayment);
	}

	@Override
	public void processCEB412Message() {
		logger.error("海关支付回执信息处理-CEB412Message");
		FtpUtils ftp = new FtpUtils(customsConfig.get("ftp.ip"), Integer.valueOf(customsConfig.get("ftp.port")),
				customsConfig.get("ftp.username"), customsConfig.get("ftp.password"),
				customsConfig.get("ftp.receive.path"));
		String locaFilePath = customsConfig.get("local.receive.path");
		List<String> fileNameList = new ArrayList<String>();
		try {
			fileNameList = ftp.getFileName(customsConfig.get("ftp.receive.path"), locaFilePath);
		} catch (Exception e) {
			logger.error("海关支付回执信息FTP处理异常-", e);
		}
		for (String fileName : fileNameList) {
			String file = locaFilePath + fileName;
			CEB412Message ceb412Messasge = new CEB412Message();
			logger.error("海关支付回执--1: "+file);
			try {
				ceb412Messasge = JaxbTools.readString(CEB412Message.class, file);
			} catch (Exception e) {
				logger.error("海关支付回执信息xml文件to -CEB412Message 异常", e);
			}
			logger.error("海关支付回执--2: "+ceb412Messasge.toString());
			if (ceb412Messasge != null) {
				PaymentReturn paymentReturn = ceb412Messasge.getPaymentReturn();
				logger.error("海关支付回执--3: "+paymentReturn.toString());
				logger.error("海关支付回执--3: "+paymentReturn.getPayTransactionId());
				CbRmbCustomsPayment payment = cbRmbCustomsPaymentDAO.findByGuid(ceb412Messasge.getGuid());
				if (null != payment) {
					payment.setReturnInfo(paymentReturn.getReturnInfo());
					payment.setReturnStatus(paymentReturn.getReturnStatus());
					payment.setReturnTime(paymentReturn.getReturnTime());
					payment.setLastUpdateTime(new Date());
					cbRmbCustomsPaymentDAO.update(payment);
					logger.error("海关支付回执--4: 完成"+payment.getReturnInfo());
				}
			}
		}

	}
	
	@Override
	public void processCEB412MessageDalian() {
		logger.error("海关支付回执信息处理-CEB412Message Dalian");
		FtpUtils ftp = new FtpUtils(customsConfig.get("ftp.dalian.ip"), Integer.valueOf(customsConfig.get("ftp.dalian.port")),
				customsConfig.get("ftp.dalian.username"), customsConfig.get("ftp.dalian.password"),
				customsConfig.get("ftp.receive.dalian.path"));
		String locaFilePath = customsConfig.get("local.receive.dalian.path");
		List<String> fileNameList = new ArrayList<String>();
		try {
			fileNameList = ftp.getFileName(customsConfig.get("ftp.receive.dalian.path"), locaFilePath);
		} catch (Exception e) {
			logger.error("海关支付回执信息FTP处理异常 Dalian-", e);
		}
		for (String fileName : fileNameList) {
			String file = locaFilePath + fileName;
			CEB412Message ceb412Messasge = null;
			logger.error("海关支付回执 Dalian--1: "+file);
			try {
				ceb412Messasge = JaxbTools.readString(CEB412Message.class, file);
			} catch (Exception e) {
				logger.error("海关支付回执信息xml文件to -CEB412Message Dalian异常", e);
			}
			if (ceb412Messasge != null) {
				logger.error("海关支付回执--2 Dalian: "+ceb412Messasge.toString());
				PaymentReturn paymentReturn = ceb412Messasge.getPaymentReturn();
				logger.error("海关支付回执--3 Dalian: "+paymentReturn.toString());
				logger.error("海关支付回执--3 Dalian: "+paymentReturn.getPayTransactionId());
				CbRmbCustomsPayment payment = cbRmbCustomsPaymentDAO.findByGuid(ceb412Messasge.getGuid());
				if (null != payment) {
					payment.setReturnInfo(paymentReturn.getReturnInfo());
					payment.setReturnStatus(paymentReturn.getReturnStatus());
					payment.setReturnTime(paymentReturn.getReturnTime());
					payment.setLastUpdateTime(new Date());
					cbRmbCustomsPaymentDAO.update(payment);
					logger.error("海关支付回执--4 Dalian: 完成"+payment.getReturnInfo());
				}
			}
		}

	}

}
